package Json2RDF.Surface2RDF.SurfaceWriter;

import Json2RDF.Json2Java.OntologyClasses.Part;
import Json2RDF.Surface2RDF.IndexTable.PartId2ResourceIndexTable;
import RDFManager.PropertySet;
import RDFManager.RDFHead;
import RDFManager.RDFReader;
import RDFManager.ResourceSet;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

import java.util.List;
import java.util.stream.IntStream;

public class PartWriter {
  public static void writeList(List<Part> partList) {

    IntStream.range(0, partList.size()).forEach(i -> {
      write(partList.get(i), "part" + (i + 1));
      PartId2ResourceIndexTable.partResourceIdxTbl.computeIfAbsent(partList.get(i).getID(), v -> RDFReader.model.getResource(RDFHead.namespace + "part" + (i + 1)));
    });
  }


  private static void write(Part part, String localName) {
    Resource inst = RDFReader.model.createResource(RDFHead.namespace + localName);
    RDFReader.model.add(inst, RDF.type, ResourceSet.Part);
    inst.addLiteral(PropertySet.hasID, part.getID());
  }
}
